package com.invengo.module_check.ui.fragment.first_check.widget;

import android.annotation.SuppressLint;
import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.library_rfid.classify.RFIDCallBackListener;
import com.invengo.library_rfid.entity.TagInfo;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ChangeFistViewModel extends BaseViewModel<BusinessRepsitory> {
    public RFIDCallBackListener rfidService = new RFIDCallBackListener(AnalyzeImpl.TO_BOOK,
            tagInfo -> onScanResult(tagInfo),
            state -> isReading.set(state));
    public SingleLiveEvent<String> newBarCode = new SingleLiveEvent<>();
    public SingleLiveEvent<BookBean> bookBean = new SingleLiveEvent<>();

    public ChangeFistViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
        registerRFID(rfidService);
        RxBus.getDefault().postSticky(AnalyzeImpl.TO_BOOK);
    }

    public void onScanResult(TagInfo tagInfo) {
        switch (tagInfo.getRfidModel()) {
            case TID:
                if (tagInfo.getScanModel() == AnalyzeImpl.TO_SHELF) {
//                    getShelfByTid(tagInfo.getTag());
                } else
                    getBookByTid(tagInfo.getTag());
                break;
            case EPC:
            case U_DATA:
                if (tagInfo.getScanModel() == AnalyzeImpl.TO_SHELF) {
//                    getShelfByCode(tagInfo.getTag());
                } else
                    getBookByCode(tagInfo.getTag());
                break;
        }
    }

    public UIChangeObservable ui = new UIChangeObservable();

    public class UIChangeObservable {
        public SingleLiveEvent<ShelfBean> changeBookEvent = new SingleLiveEvent<>();
    }


    /**
     * 按Tid查询书本
     *
     * @param tid 标签
     */
    private void getBookByTid(String tid) {
        model.getBookByTid(tid)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bean -> bookBean.postValue(bean));
    }

    /**
     * 按Code查询书本
     *
     * @param code 标签
     */
    private void getBookByCode(String code) {
        model.getBookByCode(code)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bookBean -> {
                    this.bookBean.postValue(bookBean);
                });
    }



    private ShelfBean currentShelfBean;

    public BindingCommand deleteBook = new BindingCommand(() -> {
        bookBean.postValue(null);
        newBarCode.postValue(null);
    });

    public BindingCommand changeFirst = new BindingCommand(() -> {

        if (currentShelfBean != null) {
            if (bookBean.getValue() == null) {
                if (!TextUtils.isEmpty(newBarCode.getValue())){
                    ToastUtils.showShort("该首书信息不完整，请扫描或输入查找");
                    return;
                }
                currentShelfBean.setFirstBookName("");
                currentShelfBean.setFirstBookID("");
                currentShelfBean.setBookIndex("");
                currentShelfBean.setPretendIndexNum("");
            } else {
                BookBean bean = bookBean.getValue();
                currentShelfBean.setFirstBookName(bean.getBookName());
                currentShelfBean.setFirstBookID(bean.getBarCode());
                currentShelfBean.setBookIndex(bean.getCallNo());
                currentShelfBean.setPretendIndexNum(bean.getPretendCallNo());
            }
            ui.changeBookEvent.postValue(currentShelfBean);
        }
    });

    public void updateDialogBook(ShelfBean shelfBean) {
        this.currentShelfBean = shelfBean;
        BookBean bean = new BookBean();
        bean.setBookName(shelfBean.getFirstBookName());
        bean.setBarCode(shelfBean.getFirstBookID());
        bean.setCallNo(shelfBean.getBookIndex());
        bean.setPretendCallNo(shelfBean.getPretendIndexNum());
        bookBean.postValue(bean);
        newBarCode.postValue(shelfBean.getFirstBookID());
    }

    public BindingCommand<String> searchCommand = new BindingCommand<String>(s -> {
        getUC().getHideKeyboardEvent().call();
        getBookByCode(s);
    });



    public BindingCommand<String> changeVisiable = new BindingCommand<>(s -> {
        newBarCode.postValue(s);
    });


    public BindingCommand cleanNewBookInfo = new BindingCommand(() -> {
        bookBean.postValue(null);
    });
}